/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "devmgr.h"
#include <parameter.h>
#include <string>

namespace SwanLink {
namespace Finance {

const  std::string dev_prefix = "finance.sys.dev.";
#define MAX_PARAM_LEN 64

bool DevMgr::GetDevConfig(const std::string &node, std::string &name, std::string &dev, int &mode)
{
    std::string key = dev_prefix+node;
    auto id_name = FindParameter((key+".name").c_str());
    auto id_mode = FindParameter((key+".mode").c_str());
    auto id_dev = FindParameter((key+".dev").c_str());
    if (id_name == -1 || id_mode == -1 || id_dev == -1)
        return false;
    char buffer[MAX_PARAM_LEN];
    GetParameterValue(id_name, buffer, MAX_PARAM_LEN);
    name = buffer;
    GetParameterValue(id_dev, buffer, MAX_PARAM_LEN);
    dev = buffer;
    GetParameterValue(id_mode, buffer, MAX_PARAM_LEN);
    mode = atoi(buffer);
    return true;
}

bool DevMgr::GetDevPath(const std::string &node, std::string &dev)
{
    std::string key = dev_prefix+node;
    auto id_dev = FindParameter((key+".dev").c_str());
    if (id_dev == -1) {
        return false;
    }
    char buffer[MAX_PARAM_LEN];
    GetParameterValue(id_dev, buffer, MAX_PARAM_LEN);
    dev = buffer;
    return true;
}
}  // namespace Finance
}  // namespace SwanLink
